package top.superflow.flow.runtime.mapper

import top.superflow.exception.Exceptable
import top.superflow.flow.runtime.flow.{FlowRequest, FlowResponse}
import top.superflow.flow.runtime.process.ProcessRequest
import top.superflow.lang.CollectionUtils

class JsonMappingRuntimeMapper(mappingRule : Map[String, String]) extends RuntimeMapper with Exceptable{

  if(CollectionUtils.isEmpty(mappingRule)){
    throwRequestUnsupportedException("The mapping rule should not be empty")
  }

  protected def generatedProcessRequest(flowRequest: FlowRequest, flowResponse: FlowResponse): ProcessRequest = {

    val processRequest = extractParamByAutoSearching(flowRequest, flowResponse)
    val paramsByRule = MapperSyntaxParser.getInstance.extractParameters(flowRequest, flowResponse, mappingRule)
    processRequest.addParameters(paramsByRule)

    processRequest
  }


  private def extractParamByAutoSearching(flowRequest: FlowRequest, flowResponse: FlowResponse): ProcessRequest ={
    AutoMatchUtils.extractProcessRequestByAutoMatch(flowRequest, flowResponse, getCurrentInputSchema)
  }



}
